package com.kpx.app.utils.dbutils;

import android.text.TextUtils;

import com.kpx.app.Application;
import com.kpx.app.bean.SearchHistory;
import com.kpx.app.bean.User;
import com.kpx.app.bean.UserDBModel;
import com.kpx.app.utils.datautils.FastJsonUtils;
import com.kpx.app.utils.datautils.LogUtils;

import net.tsz.afinal.FinalDb;

import java.util.List;

/**
 * 创建时间： 2015/7/8 14:22
 * 作者：Ryan
 * 类描述：信息保存、删除、更新操作工具类
 */
public class DBOperateUtils {

    public static final String TAG = "DBOperateUtil";
    private static FinalDb finalDb = FinalDb.create(Application.instance, false);

    public static FinalDb getFinalDb() {
        return finalDb;
    }

    /**
     * 保存用户
     *
     * @param user
     */
    public static void save(User user) {
        updateUser(user);
    }

    public static void saveUsers(List<User> users) {
        if (users != null && users.size() > 0) {
            LogUtils.i(TAG, "保存用户数据： " + users);
            for (User user : users) {
                save(user);
            }
        }
    }

    /**
     * 查找用户
     *
     * @param dmID
     * @return
     */
    public static User findUser(String dmID) {
        if (null != finalDb) {
            List<UserDBModel> UserDBModels = finalDb.findAllByWhere(UserDBModel.class, "dmId=" + dmID);
            if (UserDBModels.size() == 0) {
                LogUtils.i(TAG, "未找到当前用户 : " + dmID);
                return null;
            }
            UserDBModel userDBModel = UserDBModels.get(0);
            if (UserDBModels.size() > 1) {
                deleteUser(dmID);
                DBOperateUtils.getFinalDb().save(userDBModel);
            }
            return FastJsonUtils.json2Object(userDBModel.getUserJson(), User.class);
        }
        return null;
    }

    /**
     * 更新用户
     *
     * @param user
     */
    public static void updateUser(User user) {
        if (user == null) {
            return;
        }
        String dmID = user.getDmId() + "";
        String userJson = FastJsonUtils.object2Json(user);
        UserDBModel userDBModel = new UserDBModel(userJson, user.getDmId());
        User foundUser = findUser(dmID);
        if (foundUser == null) {
            finalDb.save(userDBModel);
        } else {
            finalDb.update(userDBModel, "dmId=" + user.getDmId());
        }
    }

    /**
     * 删除用户
     *
     * @param dmID
     */
    public static void deleteUser(String dmID) {
        finalDb.deleteByWhere(UserDBModel.class, "dmId=" + dmID);
    }

    /**
     * 删除用户
     *
     * @param dmID
     */
    public static void deleteUser(long dmID) {
        finalDb.deleteByWhere(UserDBModel.class, "dmId=" + dmID);
    }

    /**
     * 保存搜索历史记录
     *
     * @param searchHistory
     */
    public static void saveSearchHistory(SearchHistory searchHistory) {
        try {
            if (null != searchHistory && null != finalDb) {
                deleteSearchHistoryByName(searchHistory.getSearchName());
                finalDb.save(searchHistory);
                List<SearchHistory> searchHistoryList = getAllSearchHistory();
                if (null != searchHistoryList && searchHistoryList.size() > 10) {
                    DBOperateUtils.deleteSearchHistoryById(searchHistoryList.get(9));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据搜索关键字查找历史记录
     *
     * @param name
     * @return
     */
    public static void deleteSearchHistoryByName(String name) {
        if (!TextUtils.isEmpty(name)) {
            finalDb.deleteByWhere(SearchHistory.class, "searchName='" + name + "'");
        }
    }

    /**
     * 获取所有搜索历史记录
     *
     * @return
     */
    public static List<SearchHistory> getAllSearchHistory() {
        if (null != finalDb) {
            return finalDb.findAllByWhere(SearchHistory.class, null, "id desc");
        }
        return null;
    }

    /**
     * 获取所有搜索历史记录 排序
     *
     * @return
     */
    public static List<SearchHistory> getAllSearchHistoryByOrder() {
        try {
            if (null != finalDb) {
                return finalDb.findAllByWhere(SearchHistory.class, null, "id desc limit 10");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据id删除搜索历史记录
     *
     * @param searchHistory
     */
    public static void deleteSearchHistoryById(SearchHistory searchHistory) {
        if (null != finalDb) {
//            finalDb.deleteByWhere(SearchHistory.class, "id < " + searchHistory.getId());
        }
    }

    /**
     * 清空搜索历史记录
     */
    public static void deleteAllSearchHistory() {
        try {
            if (null != finalDb) {
                finalDb.deleteByWhere(SearchHistory.class, "1=1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
